package com.bear.hospital.service.login.handle.strategy;

import com.bear.hospital.entity.LoginInfo;
import com.bear.hospital.mapper.DoctorMapper;
import com.bear.hospital.mapper.LoginMapper;
import com.bear.hospital.pojo.Doctor;
import com.bear.hospital.pojo.LoginMap;
import com.bear.hospital.service.doctor.DoctorService;
import com.bear.hospital.service.login.handle.LoginStrategy;
import com.bear.hospital.utils.JwtUtil;
import com.bear.hospital.utils.ResponseData;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author： zmwx
 * @Date： 2025/1/23 17:14
 * @Describe：
 */
@Component
public class LoginDoctor implements LoginStrategy {
    @Override
    public String getType() {
        return "doctor";
    }

    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private DoctorService doctorService;
    @Resource
    LoginMapper loginMapper;

    @Override
    public ResponseData login(LoginMap loginInfo) {
        Doctor doctor = this.doctorService.login(loginInfo.getTid(), loginInfo.getPassword());

        if (ObjectUtils.isNotEmpty(doctor)) {
            Map<String, String> map = new HashMap<>();
            map.put("dName", doctor.getdName());
            map.put("dId", String.valueOf(doctor.getdId()));
            String token = JwtUtil.getToken(map);
            map.put("token", token);
            map.put("loginUrl", loginInfo.getLoginUrl());
            map.put("imgUrl",doctor.getProfilePhotoUrl());
            //response.setHeader("token", token);
            return ResponseData.success("登录成功", map);
        } else {
            return ResponseData.fail("登录失败，密码或账号错误");
        }
    }

    @Override
    public ResponseData setProfilePhoto(LoginInfo loginInfo,String phoneUrl) {
        return null;
    }

    @Override
    public void addUser(LoginMap loginMap) {
        loginMapper.insertLoginMap(loginMap);
    }
}
